package com.lsh.payment.core.util.pay.dmallpay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.lsh.payment.api.model.dmallpay.DMallNotifyRequest;
import com.lsh.payment.api.model.dmallpay.DMallNotifyRequestParam;
import com.lsh.payment.core.util.pay.wxpay.SignUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Project Name: lsh-payment
 * Author: panxudong
 * 北京链商电子商务有限公司
 * Desc: 类功能描述
 * Package Name: com.lsh.payment.core.util.pay.dmallpay
 * Time: 2017-11-15 下午5:46
 */
public class DMallSignature {

    public static final <T> boolean checkSign(T obj, String key, String sign) {
        String targetSign = sign(obj, key);
        return sign.equals(targetSign);
    }

    public static final <T> String sign(T obj, String key) {
        Map<String, String> stringParams = new HashMap<>();

        JSONObject jo = JSONObject.parseObject(JSON.toJSONString(obj), Feature.OrderedField);
        for (String k : jo.keySet()) {
            Object value = jo.get(k);
            if (value == null) {
                continue;
            }

            String valueStr;
            if (isBaseDataType(value.getClass())) {
                valueStr = String.valueOf(value);
            } else {
                valueStr = JSON.toJSONString(value);
            }

            if (!StringUtils.hasText(valueStr)) {
                continue;
            }

            stringParams.put(k, valueStr);
        }

        return SignUtils.createSign(stringParams, key);
    }

    private static final boolean isBaseDataType(Class clazz) {
        return (clazz.equals(String.class) || clazz.equals(Long.class) || clazz.equals(Integer.class)
                || clazz.equals(Byte.class) || clazz.equals(Double.class) || clazz.equals(Float.class)
                || clazz.equals(Character.class) || clazz.equals(Short.class) || clazz.equals(BigDecimal.class)
                || clazz.equals(BigInteger.class) || clazz.equals(Boolean.class) || clazz.equals(Date.class)
                || clazz.isPrimitive());
    }

    public static void main(String[] args) throws Exception {
        Map<String, String> params = new HashMap<>();
        params.put("sign", "371B2F2C8B5D7CABD4FD7A7468A7BBAF");
        params.put("param", "{\"actualAmount\":27520,\"cartId\":\"0e3a15b44a004db6b248f00551f42e8e\",\"nonceStr\":\"aee7a2d8-c4f4-4c8d-a250-329b49fe3432\",\"orderId\":365985213,\"originAmount\":27520,\"paymentTime\":\"2017-08-08 10:41:50\",\"paymentType\":888,\"thirdOrderId\":\"1708081019190002448\"}");

        DMallNotifyRequest request = new DMallNotifyRequest();
        request.setSign(params.get("sign"));
        request.setParam(JSON.parseObject(params.get("param"), DMallNotifyRequestParam.class));
        System.out.println(JSON.toJSONString(request.getParam()));

        System.out.println(sign(request.getParam(), "LSEa1hP+AsFQ2bd"));
    }

}
